package com.jss.lanouhn.fristapp.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.SearchActivity;
import com.jss.lanouhn.fristapp.adapters.SelecterAdapter;
import com.jss.lanouhn.fristapp.modles.SelecterInfo;
import com.ybao.pullrefreshview.layout.BaseFooterView;
import com.ybao.pullrefreshview.layout.BaseHeaderView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 热门页面的Fragment
 * Created by lanouhn on 16/8/23.
 */
public class Fragment_Selecter extends Fragment implements BaseHeaderView.OnRefreshListener, BaseFooterView.OnLoadListener{
    private String url="http://api.liwushuo.com/v2/items?gender=1&generation=2&limit=50&offset=0";
    private SelecterInfo selecterInfo;
    private RecyclerView recyclerView;
    private SelecterAdapter selecterAdapter;
    private ImageView iv_search;
//    BaseHeaderView headerView;
//    BaseFooterView footerView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_selecter,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initForDateWeb();
    }
    private void initView(){
        recyclerView= (RecyclerView) getView().findViewById(R.id.rv_selecter);
        iv_search= (ImageView) getView().findViewById(R.id.iv_bar_seacher);
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });

//        headerView= (BaseHeaderView) getView().findViewById(R.id.header);
//        footerView= (BaseFooterView) getView().findViewById(R.id.footer);
//        headerView.setOnRefreshListener(this);
//        footerView.setOnLoadListener(this);
    }

    private void initForDateWeb(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client=new OkHttpClient();
                Request request=new Request.Builder()
                        .url(url)
                        .build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result=response.body().string();
                        Gson gson=new Gson();
                        selecterInfo=gson.fromJson(result,SelecterInfo.class);
                        if (selecterInfo.getData().getItems().size()>0){
                            handler.sendEmptyMessage(1);
                        }else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();

    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    GridLayoutManager gManager=new GridLayoutManager(getContext(),2);
                    gManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(gManager);
                    selecterAdapter=new SelecterAdapter(getContext(),selecterInfo);
                    recyclerView.setAdapter(selecterAdapter);
                    break;
                case 2:
                    Toast.makeText(getActivity(), "没有数据", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    public void onRefresh(BaseHeaderView baseHeaderView) {
//        baseHeaderView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                selecterAdapter.notifyDataSetChanged();
//                headerView.stopRefresh();
//            }
//        },2000);

    }
    @Override
    public void onLoad(BaseFooterView baseFooterView) {
//        baseFooterView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                selecterAdapter.notifyDataSetChanged();
//                footerView.stopLoad();
//            }
//        },2000);

    }

}
